package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.sys.file.FileDao;
import com.plian.system.entity.pf.EvaluationScoreDetail;
import com.plian.system.mapper.pf.EvaluationScoreDetailMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pf.IEvaluationScoreDetailService;
import com.plian.system.service.sys.file.FileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2023/1/28 10:24 AM
 * @Version: 1.0
 **/
@Slf4j
@Service
@AllArgsConstructor
public class EvaluationScoreDetailServiceImpl extends BaseServiceImpl<EvaluationScoreDetailMapper, EvaluationScoreDetail> implements IEvaluationScoreDetailService {

    private FileService fileService;

    private FileDao fileDao;

    @Override
    public boolean saveOrUpdate(EvaluationScoreDetail scoreDetail) {
        boolean flag = false;
        if (!Optional.ofNullable(scoreDetail.getId()).isPresent()) {
            flag = true;
        }
        boolean result = super.saveOrUpdate(scoreDetail);
        if (flag){
            fileService.saveAll(String.valueOf(scoreDetail.getId()), scoreDetail.getDocList());
        }else {
            fileService.edit(String.valueOf(scoreDetail.getId()), scoreDetail.getDocList());
        }
        return result;
    }

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<EvaluationScoreDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EvaluationScoreDetail::getBaseScoreId, ids);
        List<EvaluationScoreDetail> scoreDetails = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(scoreDetails)){
            return deleteLogic(scoreDetails.stream().map(EvaluationScoreDetail::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public void setFiles(List<EvaluationScoreDetail> details) {
        List<String> ids = details.stream().map(m -> m.getId() + "").collect(Collectors.toList());
        try {
            List<PageData> files = fileDao.findbyParentIds(ids);
            Map<String, List<PageData>> childMap = files.stream().collect(Collectors.groupingBy(file -> file.getString("parentId")));
            details.forEach(scoreDetail ->{
                List<HashMap> hashMaps = new ArrayList<>();
                List<PageData> file = childMap.get(scoreDetail.getId()+"");
                if (CollectionUtil.isNotEmpty(file)){
                    file.forEach(m ->{
                        fileService.setViewInfo(m);
                        hashMaps.add(m);
                    });
                }
                scoreDetail.setDocList(hashMaps);
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
